<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:c="http://java.sun.com/jsp/jstl/core"
	xmlns:p="http://primefaces.org/ui">

<h:head>
	<title>Carrinho de compras WEB</title>
</h:head>
<h:body>
	<ui:composition template="/pages/template.xhtml">
		<ui:define name="conteudo">
			<h2 align="center">Minhas listas de compras salvas</h2>

			<h:form id="form">
				<p:messages id="message_listas" />
				<p:dataTable id="listasSalvas" var="lista"
					value="#{listaComprasBean.listaSalvas}" rows="10" paginator="true"
					rowsPerPageTemplate="5,10,15" emptyMessage="Nenhuma lista salva">
					<f:facet name="header">
						<h:outputText value="Suas listas" />
					</f:facet>
					<p:column>
						<f:facet name="header">Nome da lista:</f:facet>
						<h:outputText value="#{lista.nomeLista}" id="nomeDaLista" />
					</p:column>

					<p:column>
						<p:commandButton value="Excluir"
							action="#{listaComprasBean.excluirLista}" update="@form">
							<f:setPropertyActionListener
								target="#{listaComprasBean.listaCompras.idListaCompras}"
								value="#{lista.idListaCompras}" />
							<f:setPropertyActionListener value="#{userSession.idUsuario}"
								target="#{listaComprasBean.idUser}" />	
						</p:commandButton>
					</p:column>
					<p:column>
						<h:commandLink title="Ver produtos da lista #{lista.nomeLista}">
							<p:ajax oncomplete="listaDialog.show()" update=":form_dialog:produtosLista"/>
							<h:outputText styleClass="ui-icon ui-icon-search"
								style="margin:0 auto;" />
							<f:setPropertyActionListener value="#{lista}"
								target="#{listaComprasBean.listaSelected}" />
							
						</h:commandLink>
					</p:column>
				</p:dataTable>
			</h:form>
			
			<h:form id="form_dialog">
				<p:dialog header="Produtos salvos na lista" widgetVar="listaDialog"  hideEffect="explode">
					<p:dataTable var="selectedLista" id="produtosLista"
						value="#{listaComprasBean.listaSelected.listaDeComprasAndProduto}">
						<p:column>
							<f:facet name="header">Produto:</f:facet>
							<h:outputText value="#{selectedLista.produto.nome}"
								id="nomeProduto" />
						</p:column>
						<p:column>
							<f:facet name="header">Quantidade:</f:facet>
							<h:outputText value="#{selectedLista.qtde}" id="qtdeProduto" />
						</p:column>
						<p:column>
							<f:facet name="header">Preco:</f:facet>
							<h:outputText value="#{selectedLista.produto.precoMaisBarato}"
								id="precoProduto" />
						</p:column>
						<p:column>
							<h:commandLink title="Adicionar" value="Adicionar" action="#{carrinhoBean.addCarrinho}">
								<p:ajax oncomplete="listaDialog.show()" update=":form_carrinho @form" />
								<f:setPropertyActionListener target="#{carrinhoBean.id}"
									value="#{selectedLista.produto.id}"></f:setPropertyActionListener>
								<f:setPropertyActionListener target="#{carrinhoBean.qtde}"
									value="#{selectedLista.qtde}"></f:setPropertyActionListener>
							</h:commandLink>
						</p:column>
					</p:dataTable>

				</p:dialog>
			</h:form>
		</ui:define>
	</ui:composition>
</h:body>
</html>